package com.example.miniblog.controller;

import com.example.miniblog.entity.Article;
import com.example.miniblog.entity.R;
import com.example.miniblog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    //  添加一条新的文章
    @PostMapping("/addArticle")
    public R addArticle(@RequestBody Article article){

        if(articleService.addArticle(article)){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //  得到所有文章通过作者名称
    @GetMapping("/getAll")
    public R getUserByUsername(){
        return R.ok().put("Articles",articleService.getAll());
    }

    //  得到文章通过博客id
    @GetMapping("/getArticleById/{id}")
    public R getUserByUsername(@PathVariable String id){
        return R.ok().put("Article",articleService.getArticleById(id));
    }
    //  通过id删除对应的文章
    @DeleteMapping("/removeArticleById/{id}")
    public R removeArticleById(@PathVariable String id){
        if (articleService.removeArticleById(id)){
           return R.ok();
        }else {
            return R.error();
        }
    }
    //  更新用户信息
    @PostMapping("/updateArticle")
    public R updateArticle(@RequestBody Article article){
        if(articleService.updateArticle(article)){
            return R.ok();
        }else {
            return R.error();
        }
    }
}
